/*
 * This file is part of "IdeWiki" project.
 *
 * Copyright (c) 2008 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/idewiki for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */

package ws.ebel.idewiki;

import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import ws.ebel.idewiki.editors.IdeWikiEditor;

/**
 * The activator class controls the plug-in life cycle.
 */
public class Activator extends AbstractUIPlugin {

    // The identifiers for the preferences 
    /** The Constant WIKI_URL_PREFERENCE. */
    public static final String WIKI_URL_PREFERENCE = "wiki_server";

    /** The Constant USER_NAME_PREFERENCE. */
    public static final String USER_NAME_PREFERENCE = "user_name";

    /** The Constant USER_PASSWORD_PREFERENCE. */
    public static final String USER_PASSWORD_PREFERENCE = "user_password";

    /** The Constant WIKI_EDITOR_FONT. */
    public static final String WIKI_EDITOR_FONT = "wiki_editor_font";

    /** The Constant IS_WEB_CVS_ENABLED. */
    public static final String IS_WEB_CVS_ENABLED = "is_web_cvs_enabled";

    /** The Constant IS_AUTO_LOGIN_ENABLED. */
    public static final String IS_AUTO_LOGIN_ENABLED = "is_auto_login_enabled";

    // The default values for the preferences
    /** The Constant WIKI_URL. */
    public static final String WIKI_URL = "http://localhost:8668";

    /** The Constant USER_NAME. */
    public static final String USER_NAME = "";

    /** The Constant USER_PASSWORD. */
    public static final String USER_PASSWORD = "";

    /** The plug-in ID. */
    public static final String PLUGIN_ID = "IdeWiki";

    /** The resource bundle class name. */
    public static final String RESOURCE_BUNDLE_CLASS = "ws.ebel.idewiki.IdeWikiPluginResources";

    /** The shared instance. */
    private static Activator plugin;

    /** the plugin resource bundle. */
    private ResourceBundle resourceBundle;

    /** The wiki editor list. */
    private static List<IdeWikiEditor> wikiEditorList;

    /**
     * The constructor.
     */
    public Activator() {
        plugin = this;
        wikiEditorList = new ArrayList<IdeWikiEditor>();
    }

    /**
     * Start.
     * 
     * @param context the context
     * 
     * @throws Exception the exception
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        initialiseResourceBundle();
        initialiseImageRegistry();
    }

    /**
     * Stop.
     * 
     * @param context the context
     * 
     * @throws Exception the exception
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        resourceBundle = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance.
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /**
     * Returns the resource bundle of this plugin.
     * 
     * @return the resource bundle
     */
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }

    /**
     * Get resource string for a given key.
     * 
     * @param key the key
     * 
     * @return the resource string
     */
    public static String getResourceString(String key) {
        ResourceBundle bundle = Activator.getDefault().getResourceBundle();
        try {
            return bundle.getString(key);
        } catch (MissingResourceException e) {
            return "!" + key + "!";
        }
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path.
     * 
     * @param path the path
     * 
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /**
     * Initialise the image registry.
     */
    private void initialiseImageRegistry() {
        for (int i = 0; i < IdeWikiConstants.ICONS.length; i++) {
            getImageRegistry().put(IdeWikiConstants.ICONS[i],
                    AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, IdeWikiConstants.ICONS[i]));
        }
    }

    /**
     * Initialise the resource bundle.
     */
    private void initialiseResourceBundle() {
        try {
            resourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_CLASS);
        } catch (MissingResourceException e) {
            logAndReport("Error", "The Resource Bundle is missing!!", e);
        }
    }

    /**
     * Initializes a preference store with default preference values
     * for this plug-in.
     * 
     * @param store the store
     */
    protected void initializeDefaultPreferences(IPreferenceStore store) {
        store.setDefault(WIKI_URL_PREFERENCE, WIKI_URL);
        store.setDefault(USER_NAME_PREFERENCE, USER_NAME);
        store.setDefault(USER_PASSWORD_PREFERENCE, USER_PASSWORD);
        store.setDefault(IS_WEB_CVS_ENABLED, true);
        store.setDefault(IS_AUTO_LOGIN_ENABLED, true);
    }

    /**
     * Log and report.
     * 
     * @param title the title
     * @param message the message
     * @param e the e
     */
    public void logAndReport(String title, String message, Exception e) {
        log(message, e);
        reportError(title, message);
    }

    /**
     * Log.
     * 
     * @param message the message
     */
    public void log(String message) {
        getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
    }

    /**
     * Log.
     * 
     * @param message the message
     * @param e the e
     */
    public void log(String message, Exception e) {
        getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception" + message, e));
    }

    /**
     * Report error.
     * 
     * @param title the title
     * @param message the message
     */
    public void reportError(String title, String message) {
        try {
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            MessageDialog.openError(shell, title, message);
        } catch (RuntimeException e) {
            log(e.getLocalizedMessage(), e);
        }
    }

    /**
     * Gets the wiki editor list.
     * 
     * @return the wiki editor list
     */
    public List<IdeWikiEditor> getWikiEditorList() {
        return wikiEditorList;
    }

    /**
     * Adds the wiki editor.
     * 
     * @param aWikiEditor the a wiki editor
     */
    public void addWikiEditor(IdeWikiEditor aWikiEditor) {
        wikiEditorList.add(aWikiEditor);
    }

    /**
     * Removes the wiki editor.
     * 
     * @param aWikiEditor the a wiki editor
     */
    public void removeWikiEditor(IdeWikiEditor aWikiEditor) {
        wikiEditorList.remove(aWikiEditor);
    }

    /**
     * Return the wiki url preference default.
     * 
     * @return String
     */
    public String getDefaultWikiUrlPreference() {
        return getPreferenceStore().getDefaultString(WIKI_URL_PREFERENCE);
    }

    /**
     * Return the user name preference default.
     * 
     * @return String
     */
    public String getDefaultUserNamePreference() {
        return getPreferenceStore().getDefaultString(USER_NAME_PREFERENCE);
    }

    /**
     * Return the user password preference default.
     * 
     * @return String
     */
    public String getDefaultUserPaswordPreference() {
        return getPreferenceStore().getDefaultString(USER_PASSWORD_PREFERENCE);
    }

    /**
     * @return boolean
     */
    public boolean getDefaultIsWebCvsEnabled() {
        return getPreferenceStore().getDefaultBoolean(IS_WEB_CVS_ENABLED);
    }

    /**
     * @return boolean
     */
    public boolean getIsWebCvsEnabled() {
        return getPreferenceStore().getBoolean(IS_WEB_CVS_ENABLED);
    }

    /**
     * @return boolean
     */
    public void setIsWebCvsEnabled(boolean value) {
        getPreferenceStore().setValue(IS_WEB_CVS_ENABLED, value);
    }

    /**
     * set wiki url preference.
     * 
     * @param String - preference value
     * @param value the value
     */
    public void setWikiUrlPreference(String value) {
        getPreferenceStore().setValue(WIKI_URL_PREFERENCE, value);
    }

    /**
     * set user name preference.
     * 
     * @param String - preference value
     * @param value the value
     */
    public void setUserNamePreference(String value) {
        getPreferenceStore().setValue(USER_NAME_PREFERENCE, value);
    }

    /**
     * set user password preference.
     * 
     * @param String - preference value
     * @param value the value
     */
    public void setUserPasswordPreference(String value) {
        getPreferenceStore().setValue(USER_PASSWORD_PREFERENCE, value);
    }

    /**
     * Return the wiki url preference as a String.
     * 
     * @return String
     */
    public String getWikiUrlPreference() {
        return getPreferenceStore().getString(WIKI_URL_PREFERENCE);
    }

    /**
     * Return the user name preference as a String.
     * 
     * @return String
     */
    public String getUserNamePreference() {
        return getPreferenceStore().getString(USER_NAME_PREFERENCE);
    }

    /**
     * Return the user password preference as a String.
     * 
     * @return String
     */
    public String getUserPasswordPreference() {
        return getPreferenceStore().getString(USER_PASSWORD_PREFERENCE);
    }

    /**
     * @return FontData
     */
    public FontData getWikiEditorFontPreference() {

        return PreferenceConverter.getFontData(getPreferenceStore(), WIKI_EDITOR_FONT);
    }

    /**
     * @return FontData
     */
    public FontData getDefaultWikiEditorFontPreference() {
        return PreferenceConverter.getDefaultFontData(getPreferenceStore(), WIKI_EDITOR_FONT);
    }

    /**
     * @param aEditorFont
     */
    public void setWikiEditorFontPreference(FontData aEditorFont) {
        PreferenceConverter.setValue(getPreferenceStore(), WIKI_EDITOR_FONT, aEditorFont);
    }

    /**
     * @param value
     */
    public void setIsAutoLoginEnabled(boolean value) {
        getPreferenceStore().setValue(IS_AUTO_LOGIN_ENABLED, value);
    }

    /**
     * @return
     */
    public boolean getDefaultIsAutoLoginEnabled() {
        return getPreferenceStore().getDefaultBoolean(IS_AUTO_LOGIN_ENABLED);
    }

    /**
     * @return
     */
    public boolean getIsAutoLoginEnabled() {
        return getPreferenceStore().getBoolean(IS_AUTO_LOGIN_ENABLED);
    }
}
